import groupSqlite from '@/sqlite_services/group.js'
import groupMembersSqlite from '@/sqlite_services/group_members.js'
import $store from '@/store/index.js';
import msgSqlite from "@/sqlite_services/msg.js"
export default {
	/* 获取群成员 */
	async getlist(group_id) {
		try{
			let res = await uni.$api.groups.getMemberList({group_id:group_id})
			if(res){
				//批量替换一下数据
				const transformedData = res.data.map(item => {
					return {
						group_id,
						user_id: item.user_id,
						role: item.role,
						is_muted: item.is_muted,
						group_nickname: item.group_nickname,
						user_nickname: item.user_nickname,
						avatar_url: item.avatar_url,
						user_no: item.user_no,
						user_type: item.user_type,
					};
				});
				//没办法出现了问题，先删除所有的在添加
				await groupMembersSqlite.delGroupMembersAll(group_id)
				await groupMembersSqlite.batchReplaceMembers(transformedData)
				//记录本次这个群正在维持群成员
				$store.commit('setOpenGroups',{group_id,type:'member'})
				return res.data
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 设置为群成员身份 */
	async editRole(group_id,members,role) {
		try{
			let res = await uni.$api.groups.editRole({group_id,members,role})
			if(res){
				//首先应该更新打开的store里面的值
				let updata = members.map(item => ({ user_id: item, role }));
				//更新数据库里的值
				groupMembersSqlite.batchEditGroupMembers(group_id,members,{role})
				return res.data
			}else{
				return false
			}
		}catch(e){
			console.log(e);
			throw e;
		}
	},
	/* 设置禁言等 */
	async muted(group_id,members,is_muted) {
		try{
			let res = await uni.$api.groups.muted({group_id,members,is_muted})
			if(res){
				//首先应该更新打开的store里面的值
				let updata = members.map(item => ({ user_id: item, is_muted }));
				//然后应该更新数据库里的值
				groupMembersSqlite.batchEditGroupMembers(group_id,members,{is_muted})
				return res.data
			}else{
				return false
			}
		}catch(e){
			console.log(e);
			throw e;
		}
	},
	//设置群内昵称
	async setNickName(group_id,group_nickname){
		try{
			let res = await uni.$api.groups.setNickName({group_id,group_nickname})
			if(res){
				return res
			}else{
				return false
			}
		}catch(e){
			console.log(e);
			throw e;
		}
	},
	/* 踢出群 */
	async weedout(group_id,members) {
		try{
			let res = await uni.$api.groups.weedout({group_id,members})
			if(res){
				//发送全局通知,通知页面删除好友
				uni.$emit('onGroupMemberAction',{
					type:'delMember',
					data:{
						group_id:group_id,
						user_ids:members
					}
				})
				uni.showToast({
					title:res.msg,
					icon:'none'
				})
				return res.data
			}else{
				return false
			}
		}catch(e){
			console.log(e);
			throw e;
		}
	},
	//获取在线的群成员
	async onlineGroupMember(group_id) {
		try{
			let res = await uni.$api.groups.onlineGroupMember({group_id})
			if(res){
				return res.data
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 分页获取群成员 */
	async getMemberListPage(data) {
		try{
			let res = await uni.$api.groups.getMemberListPage(data)
			if(res){
				const transformedData = res.data.list.map(item => {
					return {
						group_id:data.group_id,
						...item
					};
				});
				groupMembersSqlite.batchReplaceMembers(transformedData)
				$store.commit('setArrayMemberInfo',{
					groupId:data.group_id,
					data:transformedData
				})
				return res.data
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 分页获取群管理 */
	async getManagersListPage(data) {
		try{
			let res = await uni.$api.groups.getManagersListPage(data)
			if(res){
				groupMembersSqlite.editGroupMembersAll(data.group_id,{
					role:'member'
				})
				const transformedData = res.data.list.map(item => {
					return {
						group_id:data.group_id,
						...item
					};
				});
				groupMembersSqlite.batchReplaceMembers(transformedData)
				$store.commit('setArrayManagerInfo',{
					groupId:data.group_id,
					data:transformedData
				})
				return res.data
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 分页获取群成员 */
	async getMembersListPage(data) {
		try{
			let res = await uni.$api.groups.getMembersListPage(data)
			if(res){
				const transformedData = res.data.list.map(item => {
					return {
						group_id:data.group_id,
						...item
					};
				});
				groupMembersSqlite.batchReplaceMembers(transformedData)
				$store.commit('setArrayMemberInfo',{
					groupId:data.group_id,
					data:transformedData
				})
				return res.data
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 获取群成员信息 */
	async getGroupMemberInfo(data) {
		try{
			let res = await uni.$api.groups.getGroupMemberInfo(data)
			if(res){
				const transformedData = res.data.map(item => {
					return {
						group_id:data.group_id,
						...item
					};
				});
				console.log(transformedData);
				groupMembersSqlite.batchReplaceMembers(transformedData)
				$store.commit('setArrayMemberInfo',{
					groupId:data.group_id,
					data:transformedData
				})
				return res.data
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	/* 在群内的好友 */
	async getInGroupFriends(data) {
		try{
			let res = await uni.$api.groups.getInGroupFriends(data)
			if(res){
				return res.data
			}else{
				return false
			}
		}catch(e){
			throw e;
		}
	},
	
}